<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ResponseException;
use App\Models\Banner;
use App\Models\Goods;
use App\Models\GoodsCategory;
use App\Http\Controllers\Controller;

/**
 * API首页控制器
 * @package App\Http\Controllers\Api
 */
class HomeController extends Controller
{
	/**
	 * 首页控制器
	 * User: Clannad ~ ☆
	 * DataTime：2020/1/4 0004 19:54
	 */
	public function index()
	{
		//轮播
		$bannerList = Banner::query()
						->select('url','image')
						->where('is_show', 1)
						->get()->toArray();
		//主分类
		$categoryList = GoodsCategory::query()
						->select('id','category_name','image')
						->where([
							'is_show'    => 1,
							'show_index' => 1
						])->get()->toArray();

		$recom_category = GoodsCategory::query()
							->select('id','category_name')
							->where([
							'is_show'=>1,
							'is_recom'=>1
							])->limit(8)
							->orderBy('sort','desc')
							->get()->toArray();

		$data = [
			'banner'=>$bannerList,
			'category'=>$categoryList,
			'recom_categorys'=>$recom_category
		];

		throw new ResponseException(200,'ok',$data);
	}

	/**
	 * 分页首页推荐商品
	 * User: Clannad ~ ☆
	 * DataTime：2020/1/4 0004 19:54
	 */
	public function goods()
	{
		$where = [
			'status'=>1,
		];

		if($category_id = request()->input('category_id')){
		    $where['category_id'] = $category_id;
		}

		$goodsList = Goods::query()
				->select('id','goods_name','thumb','price','description','category_id')
				->where($where)
				->orderBy('sort','desc')
				->paginate(10);

		throw new ResponseException(200,'ok',$goodsList);
	}
}
